package auditenergetic;

import java.io.IOException;
import java.io.Writer;
import java.util.StringTokenizer;
import javax.rules.Handle;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import jess.*;

public class CreateFacts

{
    public static int rasp;
    public Rete engine;
    public Deftemplate temp1, temp2, temp3, temp4, temp5, temp6,temp7,temp8;
    public Defrule dr;
    public Fact f, f1, f2, f3, f4, f5, f6,f7;
    public int i;
    public String ss;
    public String[] a = {"nume", "prenume", "telefon","email"};
    public String[] b = {"destinatie","tip","subtip","pereti"};
    public String[] c = {"an", "pereti", "tavane", "pardoseli", "temperatura", "tipcladire",
	    		"locuitori","scop" };
    public String[] d = {"cod_proiect", "denumire_proiect", "adresa", "judet", "localitate", "proiectant"};

    public CreateFacts() {
	ss = null;
	// creaza motorul jess cu ajutorul clasei Rete
	engine = new Rete();	
    }
    
    public void init()
    {
	try {
	    engine.eval("(open \"out.txt\" file \"w\")");
	    engine.eval("(printout file \" \t Raport AUDIT ENERGETIC\" crlf crlf)");
	    
	} catch (JessException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
    
    public void readFromXML() {

	try {
	    XMLReader parser = XMLReaderFactory
		    .createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser");
	    XMLReader parser2 = XMLReaderFactory
		    .createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser");
	    XMLReader parser3 = XMLReaderFactory
		    .createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser");
	    XMLReader parser4 = XMLReaderFactory
		    .createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser");
	    XMLReader parser5 = XMLReaderFactory
		    .createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser");
	    XMLReader parser6 = XMLReaderFactory
		    .createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser");
	    parser.setContentHandler(new CEPACC());
	    parser2.setContentHandler(new Calcul_AporturiCaldura());
	    parser3.setContentHandler(new Handler_Cladiri());
	    parser4.setContentHandler(new Date_Beneficiar());
	    parser5.setContentHandler(new Date_Cladire());
	    parser6.setContentHandler(new Date_Proiect());

	    parser.parse("xml/Date_preparare_apa_calda.xml");
	    parser2.parse("xml/Calcul_AporturiCaldura.xml");
	    parser3.parse("xml/Cladire.xml");
	    parser4.parse("xml/Date_Beneficiar.xml");
	    parser5.parse("xml/Date_Cladire.xml");
	    parser6.parse("xml/Date_Proiect.xml");
	} catch (SAXException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

    }

    public void createTemplate() {
	// definim un template jess cu o denumire data si folosind instanta
	// engine-ului
	try {
	    temp1 = new Deftemplate("EnergiePrepApaCaldaConsum", "", engine);
	    temp2 = new Deftemplate("CalculAporturiCaldura", "", engine);
	    temp3 = new Deftemplate("Cladire", "", engine);
	    temp4 = new Deftemplate("Date_Beneficiar", "", engine);
	    temp5 = new Deftemplate("DateCladire", "", engine);
	    temp6 = new Deftemplate("Date_Proiect", "", engine);
	    temp7 = new Deftemplate("TempInterioara","", engine);
	    temp8 = new Deftemplate("ClasaInertie","", engine);
	    // adaugam templetului sloturi cu functia addSlot
	    temp1.addSlot("Value", Funcall.NIL, "STRING");
	    temp2.addSlot("Value2", Funcall.NIL, "STRING");

	    temp3.addSlot("destinatie", Funcall.NIL, "STRING");
	    temp3.addSlot("tip", Funcall.NIL, "STRING");
	    temp3.addSlot("subtip", Funcall.NIL, "STRING");
	    temp3.addSlot("pereti", Funcall.NIL, "STRING");

	    temp4.addSlot("nume", Funcall.NIL, "STRING");
	    temp4.addSlot("prenume", Funcall.NIL, "STRING");
	    temp4.addSlot("telefon", Funcall.NIL, "STRING");
	    temp4.addSlot("email", Funcall.NIL, "STRING");
	    
	    temp5.addSlot("an", Funcall.NIL, "STRING");
	    temp5.addSlot("pereti", Funcall.NIL, "STRING");
	    temp5.addSlot("tavane", Funcall.NIL, "STRING");
	    temp5.addSlot("pardoseli", Funcall.NIL, "STRING");
	    temp5.addSlot("temperatura", Funcall.NIL, "STRING");
	    temp5.addSlot("tipcladire", Funcall.NIL, "STRING");
	    temp5.addSlot("locuitori", Funcall.NIL, "STRING");
	    temp5.addSlot("scop", Funcall.NIL, "STRING");
	    
	    temp6.addSlot("cod_proiect", Funcall.NIL, "STRING");
	    temp6.addSlot("denumire_proiect", Funcall.NIL, "STRING");
	    temp6.addSlot("adresa", Funcall.NIL, "STRING");
	    temp6.addSlot("localitate", Funcall.NIL, "STRING");
	    temp6.addSlot("judet", Funcall.NIL, "STRING");
	    temp6.addSlot("proiectant", Funcall.NIL, "STRING");
	    temp7.addSlot("Value", Funcall.NIL, "STRING");
	    temp8.addSlot("Value", Funcall.NIL, "STRING");
	    // dupa ce am definit templateul il adaugam in motorul jess
	    engine.addDeftemplate(temp1);
	    engine.addDeftemplate(temp2);
	    engine.addDeftemplate(temp3);
	    engine.addDeftemplate(temp4);
	    engine.addDeftemplate(temp5);
	    engine.addDeftemplate(temp6);
	    engine.addDeftemplate(temp7);
	    engine.addDeftemplate(temp8);
	} catch (JessException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    public void createFacts() {

	// adaugam fapte pentru templateul creat(un fel de asserturi)
	// cream instanta fapt cu numele templateului
	try {
	    f = new Fact("EnergiePrepApaCaldaConsum", engine);
	    f1 = new Fact("CalculAporturiCaldura", engine);
	    f2 = new Fact("Cladire", engine);
	    f3 = new Fact("Date_Beneficiar", engine);
	    f4 = new Fact("DateCladire", engine);
	    f5 = new Fact("Date_Proiect", engine);
	    f6 = new Fact("TempInterioara", engine);
	    f7 = new Fact("ClasaInertie", engine);
	    f.setSlotValue("Value", new Value(new CEPACC().ComputeQ_acm(),
		    RU.INTEGER));
	    f1.setSlotValue("Value2",new Value(Calcul_AporturiCaldura.Compute(),
		    RU.INTEGER));
	    f6.setSlotValue("Value", new Value(CalculTempInt.Compute(),
		    RU.INTEGER));
	    f7.setSlotValue("Value", new Value(Handler_Cladiri.printE(),
		    RU.INTEGER));
	} catch (JessException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	StringTokenizer s1 = new StringTokenizer(Handler_Cladiri.print(), "/");
	//while (s1.hasMoreTokens()) {
	    for (int i = 0; i < b.length; i++) {
		try {
		    f2.setSlotValue(b[i], new Value(s1.nextToken(), RU.STRING));
		} catch (JessException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
	    }
	//}
	// setam valoarea pentru un slot cu valoarea returnata de functia
	// Compute_acm()
	StringTokenizer s2 = new StringTokenizer(Date_Beneficiar.printN(), "/");
	for (int i = 0; i < a.length; i++) {
	    try {
		f3.setSlotValue(a[i], new Value(s2.nextToken(), RU.STRING));
	    } catch (JessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}
	StringTokenizer s3 = new StringTokenizer(Date_Cladire.print(), "/");
	for (int i = 0; i < c.length; i++) {
	    try {
		f4.setSlotValue(c[i], new Value(s3.nextToken(), RU.STRING));
	    } catch (JessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}
	
	StringTokenizer s4 = new StringTokenizer(Date_Proiect.print(), "/");
	for (int i = 0; i < d.length; i++) {
	    try {
		f5.setSlotValue(d[i], new Value(s4.nextToken(), RU.STRING));
	    } catch (JessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}
	
	

	// adaugam faptele engineului jess pentru a fi recunoscute
	try {
	    engine.assertFact(f);
	    engine.assertFact(f1);
	    engine.assertFact(f2);
	    engine.assertFact(f3);
	    engine.assertFact(f4);
	    engine.assertFact(f5);
	    engine.assertFact(f6);
	    engine.assertFact(f7);
	    engine.executeCommand("(facts)");
	    engine.run();
	} catch (JessException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

    }

    public void createRules() {
	// comenzi jess pentru interpretarea faptelor
	try {
	    engine.eval("(printout file \" 1. INFORMATII GENERALE CU PRIVIRE LA CLADIRE\" crlf crlf)");
	    engine.eval("(printout file \" \t 1.1 DATE DE IDENTIFICARE PROIECT\" crlf)");
	    engine.eval("(defrule myrule11 " +
	    		"(Date_Proiect(cod_proiect ?x)(denumire_proiect ?y)(adresa ?z)(localitate ?t)(judet ?m)(proiectant ?k))" +
	    		"=> (printout file \"\t\t\t Cod Proiect: \"?x crlf\"\t\t\t Denumire Proiect: \"?y crlf" +
	    		"\"\t\t\t Adresa: \"?z crlf \"\t\t\t Localitate: \"?t crlf \"\t\t\t Judet: \" ?m crlf \"\t\t\t Proiectant: \" ?k crlf crlf))");
	    engine.run();
	    
	    engine.eval("(printout file \" \t 1.2 DATE DE IDENTIFICARE AUDITOR\" crlf)");
	    engine.eval("(defrule myrule0 " +
	    		"(Date_Beneficiar(nume ?x)(prenume ?z)(telefon ?w)(email ?y))" +
	    		"=> (printout file \"\t\t\t Nume: \"?x crlf\"\t\t\t Prenume: \"?z crlf" +
	    		"\"\t\t\t Telefon: \"?w crlf \"\t\t\t Email: \"?y crlf crlf))");
	    engine.run();
	    
	    engine.eval("(printout file \" \t 1.3 DATE DESPRE CLADIRE\" crlf)");
	    engine.eval("(defrule myrule1 " +
	    		"(Cladire(destinatie ?x)(tip ?y)(subtip ?z)(pereti ?w))" +
	    		"=> (printout file \"\t\t\t Destinatie: \"?x crlf\"\t\t\t Tip: \"?y crlf" +
	    		"\"\t\t\t Subtip: \"?z crlf \"\t\t\t Pereti: \"?w crlf crlf))");
	    engine.run();
	    
	    engine.eval("(printout file \" \t 1.4 CARACTERISTICI CLADIRE\" crlf)");
	    engine.eval("(defrule myrule2 " +
	    		"(DateCladire(an ?x)(pereti ?y)(tavane ?z)(pardoseli ?q)(temperatura ?w)(tipcladire ?e)(locuitori ?u)(scop ?r))" +
	    		"=> (printout file \"\t\t\t An: \" ?x crlf \"\t\t\t Pereti: \" ?y crlf \"\t\t\t Tavane: \" " +
	    		"?z crlf \"\t\t\t Pardoseli: \" ?q crlf \"\t\t\t Temperatura: \" ?w crlf " +
	    		"\"\t\t\t Tip Cladire \" ?e crlf \"\t\t\t Nr.Locuitori: \" ?u crlf \"\t\t\t" +
	    		" Scop: \" ?r crlf crlf))");
	    engine.run();
	    //reguli privind energia totala pt prepararea apei calde
	  
	    engine.executeCommand("(printout file \" 2. EVALUAREA PERFORMANTEI ENERGETICE\" crlf crlf)");
	    engine.executeCommand("(printout file \"\t 2.1 CONSUMUL PENTRU PREPARAREA APEI CALDE DE CONSUM\" crlf)");
	    engine.eval("(defrule myrule3 ?a<-(EnergiePrepApaCaldaConsum{ Value <= 1000}) " +
	    		"=> (printout file \"\t\t\t Energia totala consumata: \"?a.Value \"=>sugestie 1\"crlf crlf))");
	    engine.eval("(defrule myrule4 ?a<-(EnergiePrepApaCaldaConsum{ Value > 1000}) " +
	    		"=> (printout file \"\t\t\t Energia totala consumata: \"?a.Value \"=>sugestie 2\" crlf crlf))");
	    engine.run();
	    
	    //reguli privind calculul aporturilor de caldura
	    engine.executeCommand("(printout file \"\t 2.2 APORTUL DE CALDURA\" crlf)");
	    engine.eval("(defrule myrule5 ?a<-(CalculAporturiCaldura{ Value2 <= 500}) " +
		"=> (printout file \"\t\t\t Aportul de caldura mediu: \" ?a.Value2 \"=>sugestie 1\" crlf crlf))");
	    engine.eval("(defrule myrule6 ?a<-(CalculAporturiCaldura{ Value2 > 500}) " +
		"=> (printout file \"\t\t\t Aportul de caldura mediu: \" ?a.Value2 \"=>sugestie 2\" crlf crlf))");
	    engine.run();
	    
	    engine.executeCommand("(printout file \"\t 2.3 TEMPERATURA INTERIOARA MEDIE\" crlf)");
	    engine.eval("(defrule myrule7 ?a<-(TempInterioara{ Value <= 30}) " +
		"=> (printout file \"\t\t\t Valoare: \" ?a.Value \"=>sugestie 1\" crlf crlf))");
	    engine.eval("(defrule myrule8 ?a<-(TempInterioara{ Value > 30}) " +
		"=> (printout file \"\t\t\t Valoare: \" ?a.Value \"=>sugestie 2\" crlf crlf))");
	    engine.run();
	    
	    engine.executeCommand("(printout file \"\t 2.4 CLASA DE INERTIE A CLADIRII\" crlf)");
	    engine.eval("(defrule myrule9 ?a<-(ClasaInertie{ Value <= 150}) " +
		"=> (printout file \"\t\t\t Valoare: \" ?a.Value \" => Clasa de inertie MICA\" crlf crlf))");
	    engine.eval("(defrule myrule10 ?a<-(ClasaInertie{ Value > 150 && Value < 400}) " +
		"=> (printout file \"\t\t\t Valoare: \" ?a.Value \" => Clasa de inertie MEDIE\" crlf crlf))");
	    engine.eval("(defrule myrule11 ?a<-(ClasaInertie{ Value > 400}) " +
		"=> (printout file \"\t\t\t Valoare: \" ?a.Value \" => Clasa de inertie MARE\" crlf crlf))");
	    engine.run();
	    
	    
	    engine.eval("(close file)");
	} catch (JessException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}	
    }
    
    

    public static void main(String args[]) throws JessException, SAXException,IOException 
    {
	CreateFacts f = new CreateFacts();
	f.init();
	f.readFromXML();
	f.createTemplate();
	f.createFacts();
	f.createRules();
    }
}
